import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Iot {

    // 数据库连接信息  
    private static final String DB_URL = "jdbc:kingbase8://localhost:54321/iot";
    private static final String USER = "system";
    private static final String PASS = "123456";

    // 随机数生成器  
    private static final Random random = new Random();

    // 时间格式  
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            // 注册JDBC驱动  
            Class.forName("com.kingbase8.Driver");

            // 打开连接  
            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // 执行创建表的SQL语句（如果不存在）  
            stmt = conn.createStatement();
            String sqlCreateTable = "CREATE TABLE IF NOT EXISTS IoT_Temperature (" +
                    "id INT  PRIMARY KEY, " +
                    "measurement_time TIMESTAMP NOT NULL, " +
                    "temperature FLOAT NOT NULL)";
            stmt.executeUpdate(sqlCreateTable);
            System.out.println("Created table successfully...");

            // 准备插入数据的SQL语句  
            String sqlInsert = "INSERT INTO dpwd (time, wd) VALUES (?, ?)";
            PreparedStatement pstmt = conn.prepareStatement(sqlInsert);

            // 生成并插入随机数据  
            for (int i = 0; i < 100; i++) { // 插入100条数据作为示例  
                // 生成随机时间（在当前时间的前后一周内）  
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_YEAR, random.nextInt(7) - 3); // -3到+3天的范围，调整为前后一周  
                calendar.set(Calendar.HOUR_OF_DAY, random.nextInt(24));
                calendar.set(Calendar.MINUTE, random.nextInt(60));
                calendar.set(Calendar.SECOND, random.nextInt(60));
                Date randomTime = calendar.getTime();

                // 生成随机温度  
                float randomTemperature = random.nextFloat() * 70 - 20; // -20到50度的范围  

                // 插入数据  
                pstmt.setTimestamp(1, new java.sql.Timestamp(randomTime.getTime()));
                pstmt.setFloat(2, randomTemperature);
                pstmt.executeUpdate();
            }

            System.out.println("Inserted data successfully...");

        } catch (SQLException se) {
            // 处理JDBC错误  
            se.printStackTrace();
        } catch (Exception e) {
            // 处理Class.forName错误  
            e.printStackTrace();
        } finally {
            // 最后块用于关闭资源  
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException se2) {
            } // 什么都不做  
            try {
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    }
}